package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsTopicMapper;
import com.shop.mapper.StatMapper;
import com.shop.pojo.DtsTopic;
import com.shop.service.DtsTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DtsTopicServiceImpl
 * @Author xiao宋
 * @Date 19:57  2022/10/19
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsTopicServiceImpl implements DtsTopicService {
    @Autowired
    DtsTopicMapper topicMapper;
    @Autowired
    StatMapper statMapper;
    @Override
    public Map<String, Object> findByPage(
            Integer page, Integer limit,
            String sort, String order,
            String title, String subtitle
    ) {
        IPage<DtsTopic> iPage = new Page<>(page, limit);
        QueryWrapper<DtsTopic> w = new QueryWrapper<>();
//        判断用户是否输入值进行搜索
        if (!StringUtils.isEmpty(title)) {
            w.eq("title", title);
        }
        if (!StringUtils.isEmpty(subtitle)) {
            w.eq("subtitle", subtitle);
        }
//        判断排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }
        w.eq("deleted",false);
        topicMapper.selectPage(iPage, w);

        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return data;
    }
    @Override
    public boolean delete(DtsTopic topic) {
        topic.setDeleted(true);
        topic.setUpdateTime(new Date());
        return topicMapper.updateById(topic)>0;
    }

    @Override
    public DtsTopic update(DtsTopic topic) {
        LambdaQueryWrapper<DtsTopic>w=new LambdaQueryWrapper<>();
        w.eq(DtsTopic::getDeleted,false);
        w.eq(DtsTopic::getTitle,topic.getTitle());
        if(topicMapper.selectOne(w)!=null){
            throw new RuntimeException("专题标题重复，已经有此标题");
        }

        topic.setUpdateTime(new Date());

        topic.setGoods(null);
        topicMapper.updateById(topic);
        DtsTopic dtsTopic = topicMapper.selectOne(w);
        if (topic.getGoods()!=null){
            tostr(topic.getGoods(), dtsTopic.getId());
        }
        return topic;
    }

    @Override
    public DtsTopic creat(DtsTopic topic) {
        LambdaQueryWrapper<DtsTopic>w=new LambdaQueryWrapper<>();
        w.eq(DtsTopic::getDeleted,false);
        w.eq(DtsTopic::getTitle,topic.getTitle());
//        判断专题标题重复问题
        if(topicMapper.selectOne(w)!=null){
            throw new RuntimeException("专题标题重复，已经有此标题");
        }
        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());
        boolean k=topic.getGoods()!=null;
        String[] str=null;
        if (k){
            str=topic.getGoods();
        }
        topic.setGoods(null);
        topic.setDeleted(false);
        topicMapper.insert(topic);
        DtsTopic dtsTopic = topicMapper.selectOne(w);
        if (k){
            tostr(str,dtsTopic.getId());
        }
        return topic;
    }

    private boolean tostr(String[] str,int id){
        String goods="'"+Arrays.toString(str)+"'";
        return statMapper.updateGoods(goods,id);
    }
}
